package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gcgl.domain.GcCheckRecords;
import com.ruoyi.gcgl.domain.GcProjectSgazht;
import com.ruoyi.gcgl.domain.GcQualityRecords;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtService;
import com.ruoyi.gcgl.service.IGcQualityRecordsService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;

/**
 * 质量事故调查Controller
 * 
 * @author ruoyi
 * @date 2023-05-02
 */
@Controller
@RequestMapping("/gcgl/qualityRecords")
public class GcQualityRecordsController extends BaseController
{
    private String prefix = "gcgl/qualityRecords";

    @Autowired
    private IGcQualityRecordsService gcQualityRecordsService;
    @Autowired
    private IGcProjectSgazhtService gcProjectSgazhtService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private ISysDictDataService dictDataService;

    @RequiresPermissions("gcgl:qualityRecords:view")
    @GetMapping()
    public String records()
    {
        return prefix + "/records";
    }

    /**
     * 查询质量事故调查列表
     */
    @RequiresPermissions("gcgl:qualityRecords:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcQualityRecords gcQualityRecords)
    {
        startPage();
        gcQualityRecords.setDeleteFlag(1);
        gcQualityRecords.setCreateUser(getLoginName());
        List<GcQualityRecords> list = gcQualityRecordsService.selectGcQualityRecordsList(gcQualityRecords);
        return getDataTable(list);
    }

    /**
     * 导出质量事故调查列表
     */
    @RequiresPermissions("gcgl:qualityRecords:export")
    @Log(title = "质量事故调查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcQualityRecords gcQualityRecords)
    {
        List<GcQualityRecords> list = gcQualityRecordsService.selectGcQualityRecordsList(gcQualityRecords);
        ExcelUtil<GcQualityRecords> util = new ExcelUtil<GcQualityRecords>(GcQualityRecords.class);
        return util.exportExcel(list, "质量事故调查数据");
    }

    /**
     * 新增质量事故调查
     */
    @GetMapping("/add/{sgazhtId}")
    public String add(ModelMap mmap,@PathVariable("sgazhtId") Long sgazhtId)
    {
    	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(sgazhtId);
    	mmap.put("gcProjectSgazht", gcProjectSgazht);
    	mmap.put("checkAt", DateUtils.getDate());
        return prefix + "/add";
    }

    /**
     * 新增保存质量事故调查
     */
    @RequiresPermissions("gcgl:qualityRecords:add")
    @Log(title = "质量事故调查", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcQualityRecords gcQualityRecords)
    {
    	gcQualityRecords.setStatus(0);
    	gcQualityRecords.setDeleteFlag(1);
    	gcQualityRecords.setCreateAt(new Date());
    	gcQualityRecords.setUpdateAt(new Date());
    	gcQualityRecords.setCreateUser(getLoginName());
    	gcQualityRecords.setUpdateUser(getLoginName());
        return toAjax(gcQualityRecordsService.insertGcQualityRecords(gcQualityRecords));
    }

    /**
     * 修改质量事故调查
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcQualityRecords gcQualityRecords = gcQualityRecordsService.selectGcQualityRecordsById(id);
        mmap.put("gcQualityRecords", gcQualityRecords);
        
        SysDictData dictData = new SysDictData();
        dictData.setDictType("gcgl_qualitytype");
        List<SysDictData> objlis = dictDataService.selectDictDataList(dictData);
        String[] typelis = gcQualityRecords.getQualityType().split(",");
        for(SysDictData obj:objlis){
            for(String type:typelis){
            	if(type.equals(obj.getDictLabel())){
            		obj.setListClass("true");
            	}
            }
        }
        
        mmap.put("dictDataLis", objlis);
        return prefix + "/edit";
    }

    /**
     * 修改保存质量事故调查
     */
    @Log(title = "质量事故调查", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcQualityRecords gcQualityRecords)
    {
    	gcQualityRecords.setUpdateAt(new Date());
    	gcQualityRecords.setUpdateUser(getLoginName());
        return toAjax(gcQualityRecordsService.updateGcQualityRecords(gcQualityRecords));
    }

    /**
     * 删除质量事故调查
     */
    @RequiresPermissions("gcgl:qualityRecords:remove")
    @Log(title = "质量事故调查", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcQualityRecordsService.deleteGcQualityRecordsByIds(ids));
    }

    //审核
    @RequiresPermissions("gcgl:qualityRecords:psview")
    @GetMapping("/ps")
    public String showps(){
        return prefix + "/recordsspr";
    }
    
    @PostMapping("/pslist")
    @ResponseBody
    public TableDataInfo listps(GcQualityRecords gcQualityRecords)
    {
        startPage();
        gcQualityRecords.setStatus(0);
        gcQualityRecords.setDeleteFlag(1);
        List<GcQualityRecords> list = gcQualityRecordsService.selectGcQualityRecordsList(gcQualityRecords);
        for(GcQualityRecords obj:list){
        	if(obj.getZlwj()!=null && !obj.getZlwj().equals("")){
        	obj.setZlwj(RuoYiConfig.getOnPath()+obj.getZlwj());
        	}
        }
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/yes")
    @ResponseBody
    public AjaxResult yes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQualityRecords obj = gcQualityRecordsService.selectGcQualityRecordsById(Long.parseLong(id[i].toString()));
    		if(obj!=null){
    			GcQualityRecords gcQualityRecords = new GcQualityRecords();
    			gcQualityRecords.setId(Long.parseLong(id[i]));
    			gcQualityRecords.setStatus(1);
    			gcQualityRecords.setSpr(getLoginName());
    			gcQualityRecords.setSprAt(new Date());
    			gcQualityRecordsService.updateGcQualityRecords(gcQualityRecords);
    		}
    		
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/no")
    @ResponseBody
    public AjaxResult no(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQualityRecords obj = gcQualityRecordsService.selectGcQualityRecordsById(Long.parseLong(id[i].toString()));
    		if(obj!=null){
    			GcQualityRecords gcQualityRecords = new GcQualityRecords();
    			gcQualityRecords.setId(Long.parseLong(id[i]));
    			gcQualityRecords.setStatus(2);
    			gcQualityRecords.setSpr(getLoginName());
    			gcQualityRecords.setSprAt(new Date());
    			gcQualityRecordsService.updateGcQualityRecords(gcQualityRecords);
            	
//            	GcProjectFlow gcProjectFlow = new GcProjectFlow();
//            	gcProjectFlow.setGcProjectid(other.getGcProjectid());
//            	gcProjectFlow.setGcqthtps(0);
//            	gcProjectFlow.setUpdateAt(new Date());
//            	gcProjectFlow.setUpdateUser(getLoginName());
//            	gcProjectFlowService.updateGcProjectFlow(gcProjectFlow);
    		}
    		
    	}
    	return toAjax(1);
    }
    
    /**
     * 批准
     * @return
     */
    @RequiresPermissions("gcgl:qualityRecords:pzview")
    @GetMapping("/pz")
    public String pz(){
        return prefix + "/recordspz";
    }
    
    /**
     * 查询工程其他合同列表
     */
    @PostMapping("/pzlist")
    @ResponseBody
    public TableDataInfo pzlist(GcQualityRecords gcQualityRecords)
    {
        startPage();
        gcQualityRecords.setStatus(1);
        gcQualityRecords.setDeleteFlag(1);
        List<GcQualityRecords> list = gcQualityRecordsService.selectGcQualityRecordsList(gcQualityRecords);
        for(GcQualityRecords obj:list){
        	if(obj.getZlwj()!=null && !obj.getZlwj().equals("")){
        	obj.setZlwj(RuoYiConfig.getOnPath()+obj.getZlwj());
        	}
        }
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/pzyes")
    @ResponseBody
    public AjaxResult pzyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQualityRecords obj = gcQualityRecordsService.selectGcQualityRecordsById(Long.parseLong(id[i].toString()));
    		if(obj!=null){
    			GcQualityRecords gcQualityRecords = new GcQualityRecords();
    			gcQualityRecords.setId(Long.parseLong(id[i]));
    			gcQualityRecords.setStatus(3);
    			gcQualityRecords.setPzr(getLoginName());
    			gcQualityRecords.setPzrAt(new Date());
    			gcQualityRecordsService.updateGcQualityRecords(gcQualityRecords);
            	
//            	GcProjectFlow gcProjectFlow = new GcProjectFlow();
//            	gcProjectFlow.setGcProjectid(other.getGcProjectid());
//            	gcProjectFlow.setGcqthtfp(1);
//            	gcProjectFlow.setGcqthtps(1);
//            	gcProjectFlow.setGcqthtfk(0);
//            	gcProjectFlow.setUpdateAt(new Date());
//            	gcProjectFlow.setUpdateUser(getLoginName());
//            	gcProjectFlowService.updateGcProjectFlow(gcProjectFlow);
    		}
    		
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/pzno")
    @ResponseBody
    public AjaxResult pzno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQualityRecords obj = gcQualityRecordsService.selectGcQualityRecordsById(Long.parseLong(id[i].toString()));
    		if(obj!=null){
    			GcQualityRecords gcQualityRecords = new GcQualityRecords();
    			gcQualityRecords.setId(Long.parseLong(id[i]));
    			gcQualityRecords.setStatus(4);
    			gcQualityRecords.setPzr(getLoginName());
    			gcQualityRecords.setPzrAt(new Date());
    			gcQualityRecordsService.updateGcQualityRecords(gcQualityRecords);            	
            	
//            	GcProjectFlow gcProjectFlow = new GcProjectFlow();
//            	gcProjectFlow.setGcProjectid(other.getGcProjectid());
//            	gcProjectFlow.setGcqthtps(0);
//            	gcProjectFlow.setUpdateAt(new Date());
//            	gcProjectFlow.setUpdateUser(getLoginName());
//            	gcProjectFlowService.updateGcProjectFlow(gcProjectFlow);
    		}
    		
    	}
    	return toAjax(1);
    }

}